import { _decorator, Component, Node, Animation } from 'cc';
import { UIHeader } from '../common/UIHeader';
import { EventManager } from '../event/EventManager';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import HeroApi from '../api/HeroApi';
import { HeroList } from './HeroList';
import { Hero, Stone } from '../entity/Hero';
import { StoneTip } from './StoneTip';
import { UserInfo } from '../common/UserInfo';
import { shop } from '../shop/Shop';
const { ccclass, property } = _decorator;

@ccclass('HeroStone')
export class HeroStone extends Component {
    @property(UIHeader)
    uiHeader: UIHeader
    @property(Animation)
    animation: Animation
    @property(Node)
    one: Node
    @property(Node)
    ten: Node

    start() {

        this.uiHeader.setData('获取魂石')
        EventManager.instance.on('获取魂石close', this.onClose, this)

        this.one.on(Node.EventType.TOUCH_END, this.onOne, this)
        this.ten.on(Node.EventType.TOUCH_END, this.onTen, this)
    }

    async onOne() {

        if (UserInfo.instance.diamond < 5) {
            try {
                await UIManager.instance.confrim({
                    title: '提示',
                    content: '需要消耗5元宝，元宝不足。',
                    confrimText: '获取元宝'
                })
                UIManager.instance.clear()
                let ui = await UIManager.instance.openUI(PrefabEnum.Shop)
                let com = ui.getComponent(shop)
                com.active = '获得元宝'
            } catch (error) {

            }

            return
        }
        this.animation.play('case')
        await this.waitForSecond(1)
        await this.getStone(5)
        this.animation.play('open')
    }

    async onTen() {
        if (UserInfo.instance.diamond < 50) {
            try {
                await UIManager.instance.confrim({
                    title: '提示',
                    content: '需要消耗50元宝，元宝不足。',
                    confrimText: '获取元宝'
                })
                UIManager.instance.clear()
                let ui = await UIManager.instance.openUI(PrefabEnum.Shop)
                let com = ui.getComponent(shop)
                com.active = '获得元宝'
            } catch (error) {

            }

            return
        }
        this.animation.play('case')
        await this.waitForSecond(1)
        await this.getStone(11)

        this.animation.play('open')
    }

    async getStone(op: number) {
        await UIManager.instance.showLoading()
        let res = await HeroApi.Execute({ op: op })
        UIManager.instance.closeLoading()
        console.log(res)
        let adChip = res.data.adChip
        let chip = res.data.chip
        let items = res.data.items
        let heroList = Hero.getListInHeroYard(items)

        let stoneList: Stone[] = []

        if (chip > HeroList.instance.chip) {
            let s = new Stone()
            s.name = '通用升级魂石'
            s.count = chip - HeroList.instance.chip
            stoneList.push(s)
        }


        for (let a of heroList) {
            for (let b of HeroList.instance.heroList) {
                if (a.id == b.id) {
                    if ((a.p - b.p) > 0) {
                        let stone = new Stone()
                        stone.heroId = a.id
                        stone.heroName = a.name
                        stone.name = a.name + '强化魂石'
                        stone.count = a.p - b.p
                        stoneList.push(stone)
                    }
                }
            }
        }

        console.log(stoneList)
        let ui = await UIManager.instance.openUI(PrefabEnum.StoneTip)
        ui.getComponent(StoneTip).setData(stoneList)



        HeroList.instance.setData(res.data)

        EventManager.instance.emit(EventManager.EventType.RefreshStone, stoneList)

        UserInfo.instance.refreshVillageDetail()
    }

    onClose() {
        UIManager.instance.closeUI(PrefabEnum.HeroStone)
    }

    waitForSecond(sec: number) {
        return new Promise((resolve) => {
            this.scheduleOnce(() => {
                resolve(1)
            }, sec)
        })
    }


}


